from django.db import models


# Create your models here.
# 这里的一个类就是一张表
class Publisher(models.Model):
    name = models.CharField('出版社', max_length=50, default='')


class Book(models.Model):
    title = models.CharField("书名", max_length=50, default='', unique=True)
    price = models.DecimalField('定价', max_digits=6, decimal_places=2, default=0.0)
    pub = models.CharField('出版社', max_length=50, default='', null=False)
    market_price = models.DecimalField('零售价', max_digits=6, decimal_places=2, default=0.0)
    isActive = models.BooleanField('是否上架', default=True)
    # 一对多
    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE,default='')

    def __str__(self):
        return f"书名:{self.title},定价:{self.price},零售价:{self.market_price},出版社:{self.pub}"

    class Meta:
        db_table = 'book'


class Author(models.Model):
    name = models.CharField('姓名', default='', max_length=50, null=False)
    age = models.IntegerField('年龄', default='1', null=False)
    email = models.EmailField('邮箱', null=True)
    books=models.ManyToManyField(Book)
    def __str__(self):
        return f"id:{self.id},姓名:{self.name},年龄:{self.age},邮箱:{self.email}"

    class Meta:
        db_table = 'author'


class Wife(models.Model):
    name = models.CharField('姓名', max_length=50, default='')
    author = models.OneToOneField(Author, on_delete=models.CASCADE)
